﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using ProcurementManagement.Applcation.Command.PurchaseContractsCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.Dtos.PurchaseContracts;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;
using System.Reflection.PortableExecutable;
using static ERPPublic.ParseAPI;

namespace ProcurementManagement.Applcation.CommandHanlers.PurchaseContractsCommandHandler
{
    public class FanPurchaseContractsCommandHandler : IRequestHandler<FanPurchaseContractsCommand, FanTianPurchaseContractsDTO>
    {
        private readonly InterfaceVendorRepository _vRep;
        private readonly IPurchaseContractsRepository _pRep;
        private readonly ISourcProductContractRepository _sRep;

        public FanPurchaseContractsCommandHandler(InterfaceVendorRepository vRep, IPurchaseContractsRepository pRep, ISourcProductContractRepository sRep)
        {
            this._vRep = vRep;
            this._pRep = pRep;
            this._sRep = sRep;
        }

        public async Task<FanTianPurchaseContractsDTO> Handle(FanPurchaseContractsCommand request, CancellationToken cancellationToken)
        {
            List<Products> products = await GetProductList("", "", 1, 1000);
            var guan =(from g in await _sRep.GetAll() select g).ToList();
            var purchase=(from p in await _pRep.GetAll() select p).ToList();
            var vendor=(from v in await _vRep.GetAll() select v).ToList();
            var list = (from a in purchase
                        join v in vendor on a.VendorId equals v.VendorId
                        where a.PurchaseContractsIds == request.PurchaseContractsIds
                        select new FanTianPurchaseContractsDTO
                        {
                            PurchaseContractsIds = a.PurchaseContractsIds,
                            VendorId = a.VendorId,
                            PCNumber = a.PCNumber,
                            PCName = a.PCName,
                            PCTypes = a.PCTypes,
                            PCWhetherQuality = a.PCWhetherQuality,
                            ManpowerId = a.ManpowerId,
                            PCOurRepresentatives = a.PCOurRepresentatives,
                            PCRepresentParty = a.PCRepresentParty,
                            ProcureApplyId = a.ProcureApplyId,
                            PContractsDate = a.PContractsDate,
                            PCPreparationDate = a.PCPreparationDate,
                            PCProductPrice = a.PCProductPrice,
                            PCPreferentialPrice = a.PCPreferentialPrice,
                            PCAfterOfferPrice = a.PCAfterOfferPrice,
                            AddName = a.AddName,
                            AddTime = a.AddTime,
                            VName=v.VName,
                            productLists = (from b in products
                                            join c in guan on b.ProductId equals c.ProductId
                                            where c.PCNumber == a.PCNumber
                                            select new ProductLists
                                            {
                                                PCharacteristic = b.PCharacteristic,
                                                PNumber = b.PNumber,
                                                PName = b.PName,
                                                PSpec = b.PSpec,
                                                PUnit = b.PUnit,
                                                ProductTypeId = b.ProductTypeId,
                                                ProductId = b.ProductId,
                                                SPCNum = c.SPCNum,
                                                SPCPrice = c.SPCPrice,
                                                SPCDocumentsType=c.SPCDocumentsType,
                                                SPCTotalPrice=c.SPCTotalPrice,
                                                SPCAfterPrice=c.SPCAfterPrice,
                                                SPCAfterTotalPrice=c.SPCAfterTotalPrice,
                                                SPCArrivalDate=c.SPCArrivalDate,
                                                SPCRemark=c.SPCRemark
                                            }).ToList()
                        });
            return list.FirstOrDefault();
        }
        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }
    }
}
